﻿var job;
$(function () {
    $("#year").ligerComboBox({
        width: 60,
        value: (new Date).getFullYear(),
        valueFieldID: 'hdnYear'
    });

    $("#quarter").ligerComboBox({
        width: 80,
        cancelable: false,
        valueFieldID: 'hdnQuarter',
        data: [
              { id: 0, text: "全部" },
              { id: 1, text: "第一季度" },
              { id: 2, text: "第二季度" },
              { id: 3, text: "第三季度" },
              { id: 4, text: "第四季度" }
        ],
        onSelected: function (value) {
            var monthJson = [{ id: 0, text: "全部" }];
            switch (value) {
                case "1":
                    monthJson.push({ id: 1, text: "1" }, { id: 2, text: "2" }, { id: 3, text: "3" });
                    break;
                case "2":
                    monthJson.push({ id: 4, text: "4" }, { id: 5, text: "5" }, { id: 6, text: "6" });
                    break;
                case "3":
                    monthJson.push({ id: 7, text: "7" }, { id: 8, text: "8" }, { id: 9, text: "9" });
                    break;
                case "4":
                    monthJson.push({ id: 10, text: "10" }, { id: 11, text: "11" }, { id: 12, text: "12" });
                    break;
                default:
                    monthJson.push(
                                   { id: 1, text: "1" }, { id: 2, text: "2" }, { id: 3, text: "3" },
                                   { id: 4, text: "4" }, { id: 5, text: "5" }, { id: 6, text: "6" },
                                   { id: 7, text: "7" }, { id: 8, text: "8" }, { id: 9, text: "9" },
                                   { id: 10, text: "10" }, { id: 11, text: "11" }, { id: 12, text: "12" }
                                  );
            }
            $("#month").ligerComboBox({ width: 60, cancelable: false, valueFieldID: 'hdnMonth' }).setData(monthJson);
            $("#month").ligerComboBox().setValue("0");
        }
    });

    var month = new Date().getMonth();
    if (month < 3) {
        $("#quarter").ligerComboBox().setValue("1");
    } else if (month < 6) {
        $("#quarter").ligerComboBox().setValue("2");
    } else if (month < 9) {
        $("#quarter").ligerComboBox().setValue("3");
    } else if (month < 12) {
        $("#quarter").ligerComboBox().setValue("4");
    }

    if ($("#hdnDateType").val() != "") {
        $("#quarter").ligerComboBox().setValue("0");
        if ($("#hdnDateType").val() == "month") {
            $("#month").ligerComboBox().setValue(month + 1);
        }
    }

    bindStatus();

    var flag1 = false;
    $("#eventTypeID").ligerComboBox({
        width: 100,
        cancelable: false,
        url: "/AdverseEvent/AdverseEventList/GetAdverseEventTypeList?isAll=1&ram" + Math.random(),
        valueField: "ID",
        textField: "Name",
        valueFieldID: 'hdnEventTypeID',
        onSuccess: function (data) {
            flag1 = true;
        }
    }).setValue(0);

    var flag2 = false;
    $("#areaID").ligerComboBox({
        width: 100,
        cancelable: false,
        url: "/AdverseEvent/AdverseEventReview/GetDepartmentList?ram" + Math.random(),
        parms: { isAll: 1 },
        valueField: "value",
        textField: "text",
        valueFieldID: 'hdnAreaID',
        onSuccess: function (data) {
            if ($("#hdnDateType").val() != "") {
                $("#areaID").ligerComboBox().setValue("-1");
            }
            else {
                $("#areaID").ligerComboBox().setValue($("#hdnCurrDepCode").val());
            }
            flag2 = true;
        }
    });

    $('#btnSearch').click(function () {
        search();
    });

    var t1 = window.setInterval(function () {
        if (flag1 && flag2) {
            if (job.Title.indexOf("护理部") > -1) {
                $("#quarter").ligerComboBox().setValue(0);
                $("#areaID").ligerComboBox().setValue(-1);
            }
            $('#btnSearch').click();
            window.clearInterval(t1);
        }
    }, 500);
});

function bindStatus() {
    $.ajax({
        url: "/AdverseEvent/AdverseEventReview/GetReviewProcess?ram" + Math.random(),
        type: "post",
        async: false,
        success: function (r) {
            job = $.parseJSON(r);
            var selectValue = job.DefaultStatus;
            var JsonObj = [];
            JsonObj.push({ text: "全部", value: -1 });
            if (job.IncludeStatus == "" || job.IncludeStatus == null) return;
            var includeStatus = job.IncludeStatus.split('|');
            var statusAlias = job.StatusAlias.split('|');
            for (var ii = 0; ii < includeStatus.length; ii++) {
                JsonObj.push({ text: statusAlias[ii], value: includeStatus[ii] });
            }
            if ($("#hdnStatus").val() != "") {
                selectValue = $("#hdnStatus").val();
            }
            $("#status").ligerComboBox({
                width: 80,
                cancelable: false,
                valueField: "value",
                textField: "text",
                valueFieldID: 'hdnStatus',
                data: JsonObj,
                value: selectValue
            });
        }
    });
}

//加载列表
function initTable(queryData, pageNumber) {
    $("#maingrid").ligerGrid({
        height: '100%',
        url: '/AdverseEvent/AdverseEventReview/Search?ram=' + Math.random(),
        parms: queryData,
        page: pageNumber,
        pageSize: 30,
        rownumbers: true,
        heightDiff: -5,
        columns: [
        { display: '报告时间', name: 'CreateTime' },
        { display: '病人姓名', name: 'PatientName' },
        { display: '住院号', name: 'PatientID' },
        {
            display: '事件类型', name: 'EventTypeName', render: function (row) {
                var html = row.EventTypeName;
                if (row.Flag_Yc == 1) {
                    if (row.YsType != "") {
                        html += row.YsType;
                    }
                    else {
                        html += "（院外）";
                    }
                }
                return html;
            }
        },
        { display: '报告人', name: 'NurseName' },
        { display: '发生单位', name: 'AreaName' },
        { display: '不良事件发生时间', name: 'OccurrTime' },
        {
            display: '入院/转科时间', name: 'PatientInTime', render: function (row) {
                if (!IsEmpty(row.TransferTime)) {
                    return row.TransferTime;
                }
                else {
                    return row.PatientInTime;
                }
            }
        },
        { display: '审核时间', name: 'ReviewTime' },
        {
            display: '审核状态', name: '', render: function (row) {
                if (job.IncludeStatus == null) return;
                var html = '';
                var includeStatus = job.IncludeStatus.split('|');
                var statusAlias = job.StatusAlias.split('|');
                for (var i = 0; i < includeStatus.length; i++) {
                    if (includeStatus[i] == row.Status) {
                        html = statusAlias[i];
                        break;
                    }
                }
                return html;
            }
        },
        {
            display: '操作', name: '', minWidth: 100, render: function (row) {

                var html = '<div style="heigth:100%;width:100px;text-align: left;margin:0px auto;">';
                if (job.Status != null) {
                    for (var i = 0; i < job.Status.length; i++) {
                        var statusItem = job.Status[i];
                        if (statusItem.Value == row.Status) {
                            var viewBtns = statusItem.ViewBtns;
                            for (var ii = 0; ii < viewBtns.length; ii++) {
                                var viewBtn = viewBtns[ii];
                                var detailsViewBtns = JSON.stringify(viewBtn.DetailsViewBtns).replace(/([^\\])"/g, "$1\\'");
                                html += '<a href="javascript:void(0);" class="a-edit" onclick="' + viewBtn.Function + '(' + eval(viewBtn.Parameter) + ',\'' + detailsViewBtns + '\',' + row.Status + ',' + row.Flag_Yc + ')">' + viewBtn.BtnName + '</a>&nbsp;&nbsp;&nbsp;&nbsp;';
                            }
                            break;
                        }
                    }
                }
                html += "</div>";
                return html;
            }
        }]
    });
    GridNoData($("#maingrid"));
}

//搜索
function search(pageNumber) {
    var queryData = {
        year: $("#hdnYear").val(),
        quarter: $("#hdnQuarter").val(),
        month: $("#hdnMonth").val(),
        eventTypeID: $("#hdnEventTypeID").val(),
        status: $("#hdnStatus").val(),
        areaID: $("#hdnAreaID").val()
    }
    initTable(queryData, pageNumber);
}

//审核/编辑/查看
function edit(id, obj, status, yc) {
    if (yc == 1) {
        window.location.href = "/AdverseEvent/EditPressureSoreEvent/Index?id=" + id + "&pageType=1&detailsViewBtns=" + obj + "&status=" + status;
    }
    else {
        window.location.href = "/AdverseEvent/EditAdverseEvent/Index?id=" + id + "&pageType=1&detailsViewBtns=" + obj + "&status=" + status;
    }
}

//删除
function del(id) {
    $.ligerDialog.confirm('确定删除？', function (value) {
        if (value) {
            var url = "/AdverseEvent/AdverseEventList/Delete";
            $.ajax({
                url: url,
                type: "POST",
                cache: false,
                data: { id: id },
                success: function (data) {
                    var d = $.parseJSON(data);
                    if (d.Result == 1) {
                        $.ligerDialog.success("删除成功");
                        $('#btnSearch').click();
                    }
                    else if (d.Result == 0) {
                        $.ligerDialog.error(d.Body);
                    }
                    else {
                        $.ligerDialog.error("程序错误");
                    }
                },
                error: function () {
                    $.ligerDialog.error("程序错误");
                }
            });
        }
    });
}
